package org.example.group5back.controller;
import org.example.group5back.entity.BaseResponse;
import org.example.group5back.entity.MaterialEntity;
import org.example.group5back.entity.ResultBody;
import org.example.group5back.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
public class MaterialController {

    @Autowired
    MaterialService MaterialService;


    @RequestMapping("/saveMaterial")
    @ResponseBody
    @CrossOrigin(origins = "*",allowedHeaders = "*")
    public ResultBody saveMaterial(MaterialEntity material){
        System.out.println(material.toString());
        if(MaterialService.saveMaterial(material)){
            return new ResultBody(0,"success");
        }else{
            return new ResultBody(1,"fail");
        }
    }

    @RequestMapping("/deleteMaterial")
    @CrossOrigin(origins = "*",allowedHeaders = "*")
    public ResultBody deleteMaterial(int material_id){
        if(MaterialService.deleteMaterial(material_id)){
            return new ResultBody(0,"success");
        }else{
            return new ResultBody(1,"fail");
        }
    }

    @RequestMapping("/listMaterial")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<MaterialEntity> listMaterial(){
        List<MaterialEntity> MaterialEntityList = MaterialService.finAllMaterial();
        return new BaseResponse<MaterialEntity>(0,"ok",
                MaterialEntityList.size(),MaterialEntityList);
    }


    @RequestMapping("/selectMaterial")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<MaterialEntity>selectMaterial(@RequestParam(value="page",required=false) Integer page,
    @RequestParam(value = "limit",required = false) Integer limit){
        List<MaterialEntity> material_list = MaterialService.finAllMaterial();

        return new BaseResponse<MaterialEntity>(0,"ok",
                material_list.size(),material_list);
    }
    @RequestMapping("/selectMaterialByName")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<MaterialEntity>selectMaterialByName(
            @RequestParam(value = "material_name",required = false) String material_name
    ,@RequestParam(value = "material_productiondate",required = false) String material_productiondate
    ,@RequestParam(value = "material_factorysite",required = false) String material_factorysite
    ,@RequestParam(value = "material_type",required = false) String material_type){

        if(material_name.isEmpty()){
            System.out.println("material_name is null");
            material_name = null;
        }
        if(material_productiondate.isEmpty()){
            System.out.println("material_productiondate is null");
            material_productiondate = null;
        }
        if(material_factorysite.isEmpty()){
            System.out.println("material_contacts is null");
            material_factorysite = null;
        }
        if(material_type.isEmpty()){
            System.out.println("material_type is null");
            material_type = null;
        }

        List<MaterialEntity> material_list = MaterialService.selectMaterialByName(material_name,material_productiondate,material_factorysite,material_type);

        return new BaseResponse<MaterialEntity>(0,"ok",
                material_list.size(),material_list);
    }

}
